package com.doeis.manage.service;

import java.util.ArrayList;
import java.util.List;

import com.doeis.core.db.DBException;
import com.doeis.core.db.DBManager;
import com.doeis.core.db.DaoException;
import com.doeis.manage.bean.Catalog;
import com.doeis.manage.bean.Item;
import com.doeis.manage.dao.ItemDao;

public class ItemService {
	private static final ItemDao dao = new ItemDao();

	public List<Item> list(int user_id) throws Exception {
		
		try {
			String sql = "select * from " + Item.tableName + " where uid=? ";
			List<Object> params = new ArrayList<Object>();
			params.add(user_id);
			return dao.query(Item.class, sql, false, params.toArray());
		} catch (DBException e) {
			throw new Exception(e);
		}finally{
			DBManager.closeAllBindConnection();
		}
	}

	public Item findById(int item_id) throws Exception {
		try {
			return dao.readById(Item.class, item_id);
		} catch (DaoException e) {
			throw new Exception(e);
		}finally{
			DBManager.closeAllBindConnection();
		}
	}

	public Integer add(Item item) throws Exception {
		try {
			return dao.insertAndReturnKey(item);
		} catch (DaoException e) {
			throw new Exception(e);
		}finally{
			DBManager.closeAllBindConnection();
		}
	}

	public boolean update(Item item) throws Exception {
		try {
			return dao.update(item);
		} catch (DaoException e) {
			throw new Exception(e);
		}finally{
			DBManager.closeAllBindConnection();
		}
	}

	public boolean delete(int id) throws Exception {
		try {
			return dao.delete(Catalog.class, id);
		} catch (DaoException e) {
			throw new Exception(e);
		}finally{
			DBManager.closeAllBindConnection();
		}
	}
}
